// @flow
import React from 'react'
import MenuSidebar from 'containers/MenuSidebarContainer'
import { connect } from 'react-redux'
import { Route } from 'react-router'
import DevicePage from './DevicePage'
import CustomerPage from './CustomerPage'
import ProblemPage from './ProblemPage'
import CustomerAdd from './CustomerAdd'
import Maintenance from './Maintenance'
import Maintenance1 from 'components/Maintenance1'
import Fault from './Fault'
import Fault1 from 'components/Fault1'
import EventLog from './EventLog'
import Report from 'components/Report'
import EventLog1 from 'components/EventLog1'
import FaultInfo from 'components/FaultInfo'
import Monitor from './Monitor'
import DeviceType from './DeviceType'
import DeviceTypeAdd from './DeviceTypeAdd'
import DeviceAdd from './DeviceAdd'
import ReportAdd from './ReportAdd'
import DeviceEdit from './DeviceEdit'
import DeviceTypeEdit from './DeviceTypeEdit'
import DevicePointer from './DevicePointer'
import CustomerEditPage from './CustomerEditPage'
import MaintenanceAdd from './MaintenanceAdd'
import MaintenanceAdd1 from './MaintenanceAdd1'
import MaintenanceAdd2 from './MaintenanceAdd2'
import MaintenanceEdit from './MaintenanceEdit'
import MaintenanceEdit1 from './MaintenanceEdit1'
import ReportEdit from './ReportEdit'
import FaultAdd from './FaultAdd'
import FaultAdd1 from './FaultAdd1'
import FaultEdit from './FaultEdit'
import FaultInfoAddPage from './FaultInfoAddPage'
import FaultInfoEditPage from './FaultInfoEditPage'
import DevicePointAdd from './DevicePointAdd'
import DevicePointItem from './DevicePointItem'

// 新增业务数据组件
import ModelBase from '../../Analysis/components/ModelBase'
import TaskListPage from '../../Analysis/components/TaskListPage'
import TaskAddPage from '../../Analysis/components/TaskAddPage'
import JobListPage from '../../Analysis/components/JobListPage'
import TaskEditPage from '../../Analysis/components/TaskEditPage'
import JobFilePage from '../../Analysis/components/JobFilePage'
import ModelDetailView from 'components/ModelDetailView'
import Iframe from 'components/Iframe'

import DeviceCustomer from '../../DeviceFile/components/Customer2'

const newUrl = '/analysis'
type Props = {
  userMenu: Array<Object>,
  match: Object,
  history: Object
}

type State = {
  menu: Object,
  routes: Array<Object>
}

class Layout extends React.Component<Props, State> {
  constructor (props: Props) {
    super(props)
    this.state = {
      menu: { },
      routes: [
        <Route key={'device-point-add'} path={`${this.props.match.url}/device-point-add`} component={DevicePointAdd} />,
        <Route key={'device-point-item'} path={`${this.props.match.url}/device-point-item`}
          component={DevicePointItem} />,
        <Route key={'dtadd'} path={`${this.props.match.url}/device-type-add`} component={DeviceTypeAdd} />,
        <Route key={'dadd'} path={`${this.props.match.url}/device-add`} component={DeviceAdd} />,
        <Route key={'report-add'} path={`${this.props.match.url}/report-add`} component={ReportAdd} />,
        <Route
          key={'maintenance-edit1'}
          path={`${this.props.match.url}/report-edit`} component={ReportEdit} />,
        <Route key={'de'} path={`${this.props.match.url}/device-type-edit`} component={DeviceTypeEdit} />,
        <Route key={'device-pointer'} path={`${this.props.match.url}/device-pointer`} component={DevicePointer} />,
        <Route key={'customer-add'} path={`${this.props.match.url}/customer-add`} component={CustomerAdd} />,
        <Route key={'customer-edit'} path={`${this.props.match.url}/customer-edit`} component={CustomerEditPage} />,
        <Route key={'device-edit'} path={`${this.props.match.url}/device-edit`} component={DeviceEdit} />,
        <Route key={'maintenance-add'} path={`${this.props.match.url}/maintenance-add`} component={MaintenanceAdd} />,
        <Route key={'maintenance-add1'} path={`${this.props.match.url}/maintenance-add1`}
          component={MaintenanceAdd1} />,
        <Route key={'maintenance-add2'} path={`${this.props.match.url}/maintenance-add2`}
          component={MaintenanceAdd2} />,
        <Route
          key={'maintenance-edit'}
          path={`${this.props.match.url}/maintenance-edit`} component={MaintenanceEdit} />,
        <Route
          key={'maintenance-edit1'}
          path={`${this.props.match.url}/maintenance-edit1`} component={MaintenanceEdit1} />,
        <Route key={'fault-add'} path={`${this.props.match.url}/fault-add`} component={FaultAdd} />,
        <Route key={'fault-add1'} path={`${this.props.match.url}/fault-add1`} component={FaultAdd1} />,
        <Route key={'faultInfo-add'} path={`${this.props.match.url}/faultInfo-add`} component={FaultInfoAddPage} />,
        <Route key={'faultInfo-edit'} path={`${this.props.match.url}/faultInfo-edit`} component={FaultInfoEditPage} />,
        <Route
          key={'fault-edit'}
          path={`${this.props.match.url}/fault-edit`} component={FaultEdit} />,
        //  新增业务数据路由
        <Route key='model-detail' path={`${newUrl}/models/:id`} component={ModelDetailView} />,
        <Route key={'task-edit'} path={`${newUrl}/task-edit`} component={TaskEditPage} />,
        <Route key={'job-file'} path={`${newUrl}/job-file`} component={JobFilePage} />,
        <Route key={'device-customer'} path={`${this.props.match.url}/device-customer`} component={DeviceCustomer} />
      ]
    }
  }
  componentWillMount () {
    this.setMenu()
  }
  setMenu () {
    const mainHref = '/business'
    const menuData = this.props.userMenu[0].children.find(item => item.href === mainHref)
    const url = this.props.match.url
    // 业务数据菜单,需要预先定义好左侧菜单地址, 并且配套定义好对应组件供查询
    const srcArr = [`${url}/service`, `${url}/tasks`, `${url}/task-add`, `${url}/jobs`,
      `${url}/device-info`, `${url}/customer`, `${url}/maintenance`, `${url}/fault`,
      `${url}/event-log`, `${url}/monitor`, `${url}/device-type`, `${url}/fault1`,
      `${url}/maintenance1`, `${url}/maintenance-add1`, `${url}/faultInfo`, `${url}/report`, `${url}/problem`]
    const componentArr = [ModelBase, TaskListPage, TaskAddPage, JobListPage, DevicePage,
      CustomerPage, Maintenance, Fault, EventLog1, Monitor, DeviceType, Fault1, Maintenance1,
      MaintenanceAdd1, FaultInfo, Report, ProblemPage]

    let menu = {}
    if (!menuData) return
    //  添加二级菜单
    menuData.children.forEach(item => {
      menu[item.name] = []
      item.children.forEach(children => {
        const { href, name } = children
        const componentIndex = srcArr.indexOf(href)
        if (componentIndex > -1) {
          const exact = href === `${url}/service` || href === `${url}/device-info` ? 'exact' : ''
          menu[item.name].push([name, href, componentArr[componentIndex], exact])
        }
      })
    })
    // 寻找并添加Iframe
    // const demoMenu = ['百度', '/analysis/https://www.baidu.com', Iframe]
    // menu['任务分析'].push(demoMenu)
    menuData.children.forEach(firstMenu => {
      firstMenu.children.forEach(secMenu => {
        if (secMenu.target === 'Iframe') {
          const { href, name } = secMenu
          const IframeMenu = [ name, `${mainHref}/${href}`, Iframe ]
          menu[firstMenu.name].push(IframeMenu)
        }
      })
    })
    this.setState({ menu })
    // 获取地址,默认选中第一个
    const firstSlectItemKey = Object.keys(menu)
    let firstSlectItemSrc
    for (let i = 0, length = firstSlectItemKey.length; i < length; i++) {
      if (menu[firstSlectItemKey[i]].length) {
        firstSlectItemSrc = menu[firstSlectItemKey[i]][0][1]
        break
      }
    }
    this.props.history.replace(firstSlectItemSrc)
  }
  render () {
    return (
      <MenuSidebar menu={this.state.menu} routes={this.state.routes} style={{ background: '#f5f5f5' }} />
    )
  }
}

const mapStateToProps = state => {
  return {
    userMenu: state.menu.userMenu
  }
}
const mapDispatchToProps = {
}
export default connect(mapStateToProps, mapDispatchToProps)(Layout)
